package com.fenghuajueli.module_user.activity.bindphone;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.libbasecoreui.user.UserInfoUtils;
import com.fenghuajueli.module_route.UserModuleRoute;
import com.fenghuajueli.module_user.R;
//import com.fenghuajueli.module_user.R2;
import com.fenghuajueli.module_user.activity.UserViewModelBaseActivity;
import com.fenghuajueli.module_user.databinding.ActivityBindPhoneBinding;
import com.fenghuajueli.libbasecoreui.viewmodel.BaseViewModel;
import com.fenghuajueli.module_user.model.UserViewModel;

/**
 * 付款成功之后验证手机号。只有是用户用手机号登录的才要验证手机号是否是他的
 */
@Route(path = UserModuleRoute.USER_BIND_PHONE_ACTIVITY)
public class BindPhoneActivity extends UserViewModelBaseActivity implements View.OnClickListener {

    private boolean phoneIsInputComplete = false;
    private boolean codeIsInputComplete = false;
    private boolean passwordIsInputComplete = false;

    ActivityBindPhoneBinding binding;

    private final UserViewModel userViewModel = new UserViewModel();

    public static final int TYPE_PHONE = 0;
    public static final int TYPE_THIRD = 1;

    // 0表示手机号登录购买vip之后验证手机号使用   1表示微信登录购买vip之后绑定手机使用
    @Autowired(name = "type")
    public int actionType = -1;


    @Override
    public BaseViewModel setBaseViewModel() {
        return userViewModel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityBindPhoneBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (actionType == -1) {
            ToastUtils.showShort("参数错误！");
            finish();
            return;
        }
        if (!UserInfoUtils.getInstance().isLogin()) {
            ToastUtils.showShort("当前状态未登录");
            finish();
            return;
        }
        setTextChangeListener();
        binding.btnComplete.setOnClickListener(this);
        binding.btnSendCode.setOnClickListener(this);
        binding.ivClose.setOnClickListener(this);
        userViewModel.verificationCodeTime.observe(this, new androidx.lifecycle.Observer<Integer>() {
            @Override
            public void onChanged(Integer time) {
                binding.btnSendCode.setText(time + "s");
                binding.btnSendCode.setEnabled(false);
                if (time == 0) {
                    binding.btnSendCode.setEnabled(true);
                    binding.btnSendCode.setText("获取验证码");
                }
            }
        });
        if (actionType == TYPE_PHONE) {
            binding.tvTipsTitle.setText("激活手机号");
            binding.etInputPassword.setVisibility(View.GONE);
            if (UserInfoUtils.getInstance().isPhoneNum()) {
                String phone = UserInfoUtils.getInstance().getUserInfoEntity().getUserInfo().getPhone();
                if (!TextUtils.isEmpty(phone)) {
                    binding.etInputPhone.setText(phone);
                    phoneIsInputComplete = true;
                }
            }
        } else if (actionType == TYPE_THIRD) {
            binding.tvTipsTitle.setText("绑定手机号");
            binding.etInputPassword.setVisibility(View.VISIBLE);
        }
    }

    private void setTextChangeListener() {
        binding.etInputPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString().trim())) {
                    phoneIsInputComplete = false;
                } else {
                    phoneIsInputComplete = true;
                }
                changeButtonStatus();
            }
        });
        binding.etInputCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString().trim())) {
                    codeIsInputComplete = false;
                } else {
                    codeIsInputComplete = true;
                }
                changeButtonStatus();
            }
        });
        binding.etInputPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString().trim())) {
                    passwordIsInputComplete = false;
                } else {
                    passwordIsInputComplete = true;
                }
                changeButtonStatus();
            }
        });
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btnComplete) {
            if (userViewModel != null) {
                if (actionType == TYPE_PHONE) {
                    userViewModel.activePhone(this, UserInfoUtils.getInstance().getUserInfoEntity().getToken()
                            , binding.etInputPhone.getText().toString().trim(),
                            binding.etInputCode.getText().toString().trim());
                } else if (actionType == TYPE_THIRD) {
                    userViewModel.bindPhone(this, UserInfoUtils.getInstance().getUserInfoEntity().getToken()
                            , binding.etInputPhone.getText().toString().trim(),
                            binding.etInputCode.getText().toString().trim(),
                            binding.etInputPassword.getText().toString().trim());
                }
            }
        } else if (view.getId() == R.id.ivClose) {
            finish();
        } else if (view.getId() == R.id.btnSendCode) {
            if (userViewModel != null) {
                userViewModel.getVerificationCode(this, binding.etInputPhone.getText().toString().trim());
            }
        }
    }

    private void changeButtonStatus() {
        if (phoneIsInputComplete && codeIsInputComplete && (actionType == 0 || passwordIsInputComplete)) {
            binding.btnComplete.setBackgroundResource(R.drawable.shape_btn_can_use_bg);
            binding.btnComplete.setEnabled(true);
        } else {
            binding.btnComplete.setBackgroundResource(R.drawable.shape_btn_not_use_bg);
            binding.btnComplete.setEnabled(false);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}